package com.prd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.prd.dto.OaDeptDTO;
import com.prd.model.OaDeptDO;
import com.prd.service.OaDeptService;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * <p>
 * 部门数据表 前端控制器
 * </p>
 *
 * @author lizhaoyi
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/api/oaDept")
@Api(tags = "部门设置")
public class OaDeptController {

    @Autowired
    private OaDeptService oaDeptService;

    @ApiOperation("新增部门")
    @PostMapping("add")
    public JsonData add(@RequestBody OaDeptDTO oaDeptDTO){

        OaDeptDO deptDO = oaDeptService.getOne(new LambdaQueryWrapper<OaDeptDO>().eq(OaDeptDO::getDeptName, oaDeptDTO.getDeptName()));

        if (deptDO != null) {
            return JsonData.buildError("部门已存在。");
        }

        oaDeptDTO.setCreatedTime(LocalDateTime.now());
        oaDeptService.save(CommonUtil.dtoTransfer(oaDeptDTO, OaDeptDO.class));

        return JsonData.buildSuccess(oaDeptDTO);
    }

    @ApiOperation("更新部门")
    @PostMapping("update")
    public JsonData update(@RequestBody OaDeptDTO oaDeptDTO){

        oaDeptService.updateById(CommonUtil.dtoTransfer(oaDeptDTO, OaDeptDO.class));

        return JsonData.buildSuccess(oaDeptDTO);
    }


    @ApiOperation("删除部门")
    @PostMapping("delete")
    public JsonData delete(@ApiParam("部门id") @RequestParam Long id){

        oaDeptService.removeById(id);

        return JsonData.buildSuccess();
    }

    @ApiOperation("查询部门列表")
    @PostMapping("selectList")
    public JsonData selectList(){

        return JsonData.buildSuccess(oaDeptService.list());
    }

    @ApiOperation("查询部门")
    @PostMapping("select")
    public JsonData select(@ApiParam("部门id") @RequestParam Long id){

        return JsonData.buildSuccess(oaDeptService.getById(id));
    }

}

